import processing.core.*;
import java.util.*;
public class RectNode extends Rectangle{
	ArrayList edges;
	
	public RectNode(float x, float y, float w, float h) {
		super(x, y, w, h);
		edges = new ArrayList();
		addEdge(new Edge(this, Edge.TOP));
		addEdge(new Edge(this, Edge.BTM));
		addEdge(new Edge(this, Edge.RIGHT));
		addEdge(new Edge(this, Edge.LEFT));
	}
	
	public void addEdge(Edge e){
		if(e.parent != null)
			e.parent.removeEdge(e);
		e.index = edges.size();
		e.parent = this;
		edges.add(e);
	}
	public void removeEdge(Edge e){
		if(!edges.contains(e)) return;
		edges.remove(e.index);
		e.index = -1;
		e.parent = null;
		updateIndexes();
	}
	
	public void updateIndexes(){
		for(int i = 0; i < edges.size(); i++)
			getEdge(i).index = i;
	}
	public Edge getEdge(int i){
		return (Edge)edges.get(i);
	}
	
	public ArrayList getEdges(){
		return getEdges(-1);
	}
	public ArrayList getEdges(int e){
		if(e == -1) return edges;
		ArrayList ret = new ArrayList();
		Iterator i = edges.iterator();
		while(i.hasNext()){
			Edge edge = (Edge)i.next();
			if(edge.getSide() == e) ret.add(edge);
		}
		return ret;
	}
	
	public RectNode(float w, float h) {
		this(0, 0, w, h);
	}
	
	public void draw(PApplet pa){
		pa.rect(x, y, w, h);
	}
	
	public void moveTo(float x, float y){
		float dx = x - this.x;
		float dy = y - this.y;
		Iterator e = edges.iterator();
		while(e.hasNext()){
			((Edge)e.next()).move(dx, dy);
		}
		this.x = x;
		this.y = y;
	}
	

}
